package com.cangsan.erhai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cangsan.erhai.common.PageBean;
import com.cangsan.erhai.common.R;
import com.cangsan.erhai.entity.TabContest;
import com.cangsan.erhai.service.TabContestService;
import com.cangsan.erhai.vo.TabContestVo;
import com.cangsan.erhai.vo.TabCourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/contest")
public class TabContestController {

    @Autowired
    private TabContestService tabContestService;

    @PostMapping("/list")
    public R list(@RequestBody PageBean pageBean){
        QueryWrapper<TabContest> queryWrapper = new QueryWrapper<>();
        Date date = new Date();
        String query = pageBean.getQuery();
        if("1".equals(query) || "3".equals(query)){
            queryWrapper.gt("contest_start_time",date);
        }else if("2".equals(query)){
            queryWrapper.le("contest_start_time", date) //  le表示大于等于开始时间
                    .ge("contest_end_time", date);// ge表示小于等于结束时间
        }else if("4".equals(query)){
            queryWrapper.lt("contest_end_time",date);
        }else if("0".equals(query)){

        } else {
            queryWrapper.like("contest_title",query);
        }

        Page<TabContest> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<TabContest> pageResult = tabContestService.page(page,queryWrapper);
        Map<String,Object> result = new HashMap<>();
        List<TabContest> records = pageResult.getRecords();
        result.put("dataList",records);
        result.put("total",pageResult.getTotal());
        return R.ok(result);
    }

    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody TabContest tabContest){
        tabContestService.saveOrUpdate(tabContest);
        return R.ok();
    }


    @GetMapping("/getContestByName/{contestName}")
    public R saveOrUpdate(@PathVariable String contestName){
        QueryWrapper<TabContest> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("contest_title",contestName);
        return R.ok(tabContestService.list(queryWrapper));
    }
    @GetMapping("/{id}")
    public R delete(@PathVariable Long id) {
        tabContestService.removeById(id);
        return R.ok("删除成功");
    }

    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Long id) {
        List<TabContest> dataList = new ArrayList<>();
        dataList.add(tabContestService.getById(id));
        return R.ok(dataList );
    }

    /**
     * 查询需要预约的
     * @param
     * @return
     */
    @PostMapping("/getMakeContestList")
    public R getMakeContestList(@RequestBody PageBean pageBean) {
        Page<TabContestVo> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());

        return R.ok(tabContestService.getMakeContestList(page,null,pageBean.getQuery()));
    }

    /**
     * 查询已经预约了的
     * @param
     * @return
     */
    @PostMapping("/myMakeContestList")
    public R myMakeContestList(@RequestBody PageBean pageBean) {
        Page<TabContestVo> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());


        return R.ok(tabContestService.getMakeContestList(page,"0",pageBean.getQuery()) );
    }
}
